package com.sidneysimmons.financially.managers;

import java.util.HashMap;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;

import com.sidneysimmons.financially.Server;
import com.sidneysimmons.financially.User;

public class UserManager {
	
	private static final String CURRENT_USER_PREFERENCES = "current_user";
	private final Context context;
	
	/**
	 * Utility class for all helper methods having to do with users.
	 */
	public UserManager(Context context) {
		this.context = context;
	}
	
	/**
	 * Uses the <code>Server</code> class to send a new user to the server.
	 * 
	 * @param user - The User populated with email address and password.
	 * @param handler - The Handler used to communicate with the UI thread.
	 */
	public void addUserToServer(User user, Handler handler) {
		if (user != null) {
			String emailAddress = user.getEmailAddress();
			String password = user.getPassword();
			
			if (emailAddress != null && password != null) {
				// put data into a hash map
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("emailAddress", emailAddress);
				map.put("password", password);
				
				// send via Server object
				Server server = new Server(Server.COMMAND_add_new_user, map, handler, context);
				Thread thread = new Thread(server);
				thread.start();
			}
		}
	}
	
	/**
	 * Uses the <code>Server</code> class to tell the server to email this user a copy 
	 * of his/her password.
	 * 
	 * @param user - The email address of the user.
	 * @param handler - The Handler used to communicate with the UI thread.
	 */
	public void sendForgottenPasswordEmail(String emailAddress, Handler handler) {
		if (emailAddress != null) {
			// put data into a hash map
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("emailAddress", emailAddress);

			// send via Server object
			Server server = new Server(Server.COMMAND_forgotten_password_email, map, handler, context);
			Thread thread = new Thread(server);
			thread.start();
		}
	}

	/**
	 * Uses the <code>Server</code> class to validate a user's login information 
	 * (email address and password) from the database.
	 * 
	 * @param user - The User populated with email address and password.
	 * @param handler - The Handler used to communicate with the UI thread.
	 */
	public void validateUserLogin(User user, Handler handler) {
		if (user != null) {
			String emailAddress = user.getEmailAddress();
			String password = user.getPassword();
			
			if (emailAddress != null && password != null) {
				// put data into a hash map
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("emailAddress", emailAddress);
				map.put("password", password);
				
				// validate on the server
				Server server = new Server(Server.COMMAND_validate_user_login, map, handler, context);
				Thread thread = new Thread(server);
				thread.start();
			}
		}
	}
	
	/**
	 * Checks the SharedPreferences for email address and password.
	 * 
	 * @return true if currently logged in, false otherwise
	 */
	public static boolean isLoggedIn(Context context) {
		SharedPreferences userPrefs = context.getSharedPreferences(CURRENT_USER_PREFERENCES, 0);
		String emailAddress = userPrefs.getString("email_address", null);
		String password = userPrefs.getString("password", null);
		
		// return false if no current user exists
		if (emailAddress == null || password == null) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * Get the current user from SharedPreferences.  Returns null if 
	 * no user exists.
	 * 
	 * @return User if exists, null otherwise
	 */
	public User getCurrentUser() {
		SharedPreferences userPrefs = context.getSharedPreferences(CURRENT_USER_PREFERENCES, 0);
		String emailAddress = userPrefs.getString("email_address", null);
		String password = userPrefs.getString("password", null);
		
		// return null if no current user exists
		if (emailAddress == null || password == null) {
			return null;
		}
		
		User currentUser = new User(emailAddress, password);
		return currentUser;
	}
	
	/**
	 * Set the current user in SharedPreferences.
	 */
	public void setCurrentUser(User user) {
		if (user != null) {
			String emailAddress = user.getEmailAddress();
			String password = user.getPassword();
			
			if (emailAddress != null && password != null) {
				SharedPreferences userPrefs = context.getSharedPreferences(CURRENT_USER_PREFERENCES, 0);
				SharedPreferences.Editor userPrefsEditor = userPrefs.edit();
				
				// set email address and password in preferences
				userPrefsEditor.putString("email_address", emailAddress);
				userPrefsEditor.putString("password", password);
				userPrefsEditor.commit();
			}
		}
	}
	
	/**
	 * Removes the user that is currently logged in.  Removes 
	 * the email address and password from SharedPreferences if 
	 * they exist.
	 */
	public void removeCurrentUser() {
		SharedPreferences userPrefs = context.getSharedPreferences(CURRENT_USER_PREFERENCES, 0);
		SharedPreferences.Editor userPrefsEditor = userPrefs.edit();
		
		userPrefsEditor.remove("email_address");
		userPrefsEditor.remove("password");
		userPrefsEditor.commit();
	}
}